package zhoug.chart.renderer;

import com.github.mikephil.charting.animation.ChartAnimator;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.CombinedChart;
import com.github.mikephil.charting.charts.CombinedChart.DrawOrder;
import com.github.mikephil.charting.renderer.BubbleChartRenderer;
import com.github.mikephil.charting.renderer.CandleStickChartRenderer;
import com.github.mikephil.charting.renderer.CombinedChartRenderer;
import com.github.mikephil.charting.renderer.DataRenderer;
import com.github.mikephil.charting.renderer.LineChartRenderer;
import com.github.mikephil.charting.renderer.ScatterChartRenderer;
import com.github.mikephil.charting.utils.ViewPortHandler;
import zhoug.chart.Chart2Sdk;

/**
 * 混合图表渲染器支持顶部圆弧柱状
 * 使用 {@link BarChart#setRenderer(DataRenderer)}
 *
 * @Author 35574
 * @Date 2020/9/28
 * @Description
 */
public class MPCombinedChartRenderer extends CombinedChartRenderer {
    private static final String TAG = ">>>CustomCombinedChartRend";
    private static final boolean DEBUG = Chart2Sdk.isDebug();
    /**
     * 是否启用圆弧顶部
     */
    protected boolean arcEnable = false;
    /**
     * 堆叠时只绘制顶部一个value
     */
    private boolean mOnlyDrawValueStackedTop = false;


    public MPCombinedChartRenderer(CombinedChart chart, ChartAnimator animator, ViewPortHandler viewPortHandler) {
        this(chart, animator, viewPortHandler, false);
    }

    public MPCombinedChartRenderer(CombinedChart chart, ChartAnimator animator, ViewPortHandler viewPortHandler, boolean arcEnable) {
        super(chart, animator, viewPortHandler);
        createRenderers();
    }

    @Override
    public void createRenderers() {
        mRenderers.clear();
        CombinedChart chart = (CombinedChart) mChart.get();
        if (chart == null)
            return;
        DrawOrder[] orders = chart.getDrawOrder();
        for (DrawOrder order : orders) {
            switch (order) {
                case BAR:
                    if (chart.getBarData() != null) {
                        MPBarChartRenderer mpBarChartRenderer = new MPBarChartRenderer(chart, mAnimator, mViewPortHandler);
                        mpBarChartRenderer.setArcEnable(this.arcEnable);
                        mpBarChartRenderer.setOnlyDrawValueStackedTop(this.mOnlyDrawValueStackedTop);
                        mRenderers.add(mpBarChartRenderer);
                    }
                    break;
                case BUBBLE:
                    if (chart.getBubbleData() != null)
                        mRenderers.add(new BubbleChartRenderer(chart, mAnimator, mViewPortHandler));
                    break;
                case LINE:
                    if (chart.getLineData() != null)
                        mRenderers.add(new LineChartRenderer(chart, mAnimator, mViewPortHandler));
                    break;
                case CANDLE:
                    if (chart.getCandleData() != null)
                        mRenderers.add(new CandleStickChartRenderer(chart, mAnimator, mViewPortHandler));
                    break;
                case SCATTER:
                    if (chart.getScatterData() != null)
                        mRenderers.add(new ScatterChartRenderer(chart, mAnimator, mViewPortHandler));
                    break;
            }
        }
    }

    /**
     * 是否启用圆弧顶部
     *
     * @param arcEnable
     */
    public void setBarTopArcEnable(boolean arcEnable) {
        this.arcEnable = arcEnable;
        if (mRenderers != null && !mRenderers.isEmpty()) {
            for (DataRenderer renderer : mRenderers) {
                if (renderer instanceof MPBarChartRenderer mpBarChartRenderer) {
                    mpBarChartRenderer.setArcEnable(this.arcEnable);
                }
            }
        }
    }

    /**
     * 堆叠时只绘制顶部一个value
     *
     * @param onlyDrawValueStackedTop
     */
    public void setOnlyDrawValueStackedTop(boolean onlyDrawValueStackedTop) {
        this.mOnlyDrawValueStackedTop = onlyDrawValueStackedTop;
        if (mRenderers != null && !mRenderers.isEmpty()) {
            for (DataRenderer renderer : mRenderers) {
                if (renderer instanceof MPBarChartRenderer mpBarChartRenderer) {
                    mpBarChartRenderer.setOnlyDrawValueStackedTop(this.mOnlyDrawValueStackedTop);
                }
            }
        }
    }


}
